package com.sunrise.configuracoes.repositorio;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sunrise.configuracoes.repositorio.iface.PermissaoDao;
import com.sunrise.generico.acesso.DaoService;
import com.sunrise.generico.entidade.Permissao;
import com.sunrise.generico.visao.Filtro;

@Repository
public class PermissaoDaoImpl extends DaoService<Permissao> implements PermissaoDao {

	@SuppressWarnings("unchecked")
	public List<Permissao> listarOrdenado() {
		StringBuilder queryStr = new StringBuilder();
		queryStr.append("FROM Permissao p Order by p.nome");
		return executeQuery(queryStr.toString());
	}

	public List<Permissao> search(Permissao ex) {
		List<Permissao> list =  searchByExample(Permissao.class, ex);
		Collections.sort(list, new Comparator<Permissao>() {

			@Override
			public int compare(Permissao o1, Permissao o2) {
				return o1.getNome().compareTo(o2.getNome());
			}
		});
		return list;
	}

	public Permissao getById(Long id) {
		return getById(id, Permissao.class);
	}

	public List<Permissao> searchByExample(Permissao example) {
		List<Permissao> list =  searchByExample(Permissao.class, example);
		Collections.sort(list, new Comparator<Permissao>() {

			@Override
			public int compare(Permissao o1, Permissao o2) {
				return o1.getNome().compareTo(o2.getNome());
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Permissao> listarPorFuncionalidade(Long id) {
		StringBuilder query = new StringBuilder();
		query.append("from Permissao p ");
		if (id != null && id != 0) {
			query.append("where p.funcionalidade.id = ");
			query.append(id);
		}
		return executeQuery(query.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Permissao> buscarPorPerfil(Long idPerfil) {
		Criteria crit = getSession().createCriteria(Permissao.class);
		crit.createAlias("perfis", "perfil"); 
		crit.add(Restrictions.eq("perfil.id",idPerfil));
		return crit.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Permissao> listarPorNome(String nome) {
		Criteria crit = getSession().createCriteria(Permissao.class);
		crit.add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE));
		return crit.list();
	}
	
	@Override
	public Permissao buscarPorNome(String nome) {
		Criteria crit = getSession().createCriteria(Permissao.class);
		crit.add(Restrictions.eq("nome", nome).ignoreCase());
		return (Permissao) crit.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Permissao> buscarPorFiltros(Filtro filtro) {
		Criteria crit = getSession().createCriteria(Permissao.class);
		if (filtro.getNome() != null) {
			crit.add(Restrictions.ilike("nome", filtro.getNome(),
					MatchMode.ANYWHERE));
		}

		if (filtro.getRole() != null) {
			crit.add(Restrictions.ilike("role", filtro.getRole(),
					MatchMode.ANYWHERE));
		}
		
		if (filtro.getUrl() != null) {
			crit.add(Restrictions.ilike("url", filtro.getUrl(),
					MatchMode.ANYWHERE));
		}
		
		if (filtro.getAtivo() != null) {
			crit.add(Restrictions.eq("ativo", filtro.getAtivo().getCodigo()));
		}
		
		crit.addOrder(Order.asc("nome"));
		return crit.list();
	}

}
